package kitwee.com.kanpad.orderDetail;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.LogUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import kitwee.com.kanpad.BaseFragment;
import kitwee.com.kanpad.R;
import kitwee.com.kanpad.bean.BomAdapter;
import kitwee.com.kanpad.bean.ListDetailBean;

/**
 * @author GanQuan
 * @date 2018/1/22
 */

public class BomFragment extends BaseFragment {

    @BindView(R.id.bom_list)
    RecyclerView bomRecyclerView;
    private View layout;
    private List<ListDetailBean.ProductBOMVOListBean> bomList;
    private BomAdapter bomAdapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        layout = inflater.inflate(R.layout.bom_layout, container, false);
        ButterKnife.bind(this, layout);

        return layout;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView() {
        bomList = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        bomRecyclerView.setLayoutManager(linearLayoutManager);
        bomAdapter = new BomAdapter(getActivity(),bomList);
        bomRecyclerView.setAdapter(bomAdapter);

    }

    public static BomFragment newInstance() {
        return new BomFragment();
    }


    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onShowMessageEvent(ScannerEvent messageEvent) {
        LogUtils.e("子页面 : " + messageEvent.getResult());

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onShowMessageEvent(OrderDetailEvent orderDetailEvent) {
        LogUtils.e("物料 收到的event: " + orderDetailEvent.getListDetailBean().getProductBOMVOList());
        if(layout!=null){
            List<ListDetailBean.ProductBOMVOListBean> productBOMVOList =
                    orderDetailEvent.getListDetailBean().getProductBOMVOList();
            bomList.clear();
            bomList.addAll(productBOMVOList);
            bomAdapter.notifyDataSetChanged();
        }

    }
}
